package com.funtl.framework.java.util.common;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.funtl.framework.java.util.exception.UtilException;


/**
 * 通用Properties解析工具
 * @author lwm
 *
 */
public class CommonProperty {
	private static Logger logger = Logger.getLogger(CommonProperty.class);
	private static CommonProperty instance = new CommonProperty();
	private String path;
	
	public static CommonProperty getInstance() {
		return instance;
	}
	
	private CommonProperty() {
		path = this.getClass().getResource("/").getPath();
	}
	
	/**
	 * 加载properties文件
	 * @param fileName 文件名，只需要输入文件名即可，不需要加后缀
	 * @return Properties
	 */
	public Properties loadProperties(String fileName) throws UtilException {
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(new File(path + fileName + ".properties")));
			Properties pro = new Properties();
			pro.load(in);
			return pro;
		} catch (FileNotFoundException e) {
			logger.warn("Can not found " + fileName + ".properties file in this project.");
			throw new UtilException("loadProperties", e);
		} catch (IOException e) {
			throw new UtilException("loadProperties", e);
		}
	}
	
}
